package com.oauth.global.client.ui;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.HasWidgetInfo;
import com.oauth.global.client.beans.ApplicationClientBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class BrowseWindow extends Composite {

	private static BrowseWindowUiBinder uiBinder = GWT.create(BrowseWindowUiBinder.class);

	interface BrowseWindowUiBinder extends UiBinder<Widget, BrowseWindow> {
	}

	Map<Long, HasWidgetInfo> widgetMap;

	@UiField
	VerticalPanel vPanel;

	public BrowseWindow() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public Grid addBrowseWidget(List<ApplicationClientBean> pResult) {
		Grid grid = new Grid(3, 3);
		int row = 0, column = 0;
		for (ApplicationClientBean clientBean : pResult) {
			grid.setWidget(row, column, getApplicationPanel(clientBean));
			grid.getCellFormatter().setWidth(row, column, "2px");
			row = column == 2 ? ++row : row;
			column = column == 2 ? 0 : ++column;

		}
		return grid;
	}

	private Widget getApplicationPanel(ApplicationClientBean pClientBean) {
		HasWidgetInfo hasWidgetInfo = widgetMap.get(pClientBean.getId());
		ApplicationPanel appPanel = new ApplicationPanel(hasWidgetInfo);
		return appPanel;
	}

	public void setMaps(Map<Long, HasWidgetInfo> widgetMap) {
		this.widgetMap = widgetMap;
	}

	public void initBrowseWidget() {
		vPanel.clear();
		GlobalRemoteServiceAsync.INSTANCE.getUnSubscribedApplications(

		new OAuthAsnyCallBack<List<ApplicationClientBean>>() {
			public void onSuccess(List<ApplicationClientBean> result) {
				if (!result.isEmpty()) {
					vPanel.add(new HTML("<h3>Available Applications &raquo;</h3><hr/>"));

					vPanel.add(addBrowseWidget(result));
				}
			}
		});

		GlobalRemoteServiceAsync.INSTANCE
				.getMyApplications(new OAuthAsnyCallBack<List<ApplicationClientBean>>() {

					@Override
					public void onSuccess(List<ApplicationClientBean> result) {
						addSubscribedApps(result);
					}

					private void addSubscribedApps(List<ApplicationClientBean> result) {
						if (!result.isEmpty()) {
							vPanel.add(new HTML("<h3>Subscribed Applications &raquo;</h3><hr/>"));
							vPanel.add(new HTML(
									"<div>Note: If you face any problem with giving permission to a application,click <i>sign in</i> button to get fresh permission</div>"));
							for (ApplicationClientBean clientBean : result) {
								vPanel.add(getSubscribedApplication(clientBean));
							}
						}
					}

					private Widget getSubscribedApplication(ApplicationClientBean clientBean) {
						HasWidgetInfo hasWidgetInfo = widgetMap.get(clientBean.getId());
						SubscribedApplication application = new SubscribedApplication(hasWidgetInfo);
						return application;
					}
				});
	}

}